// Copyright Gary Evans 2006.

using System;
using System.Windows.Forms;

using Tao.OpenGl;

namespace Taumuon.Jabuka.MainApp
{
    public partial class Form1 : Form
    {
        #region Member Variables

        private World world = null;

        #endregion

        /// <summary>
        /// The Application's main form.
        /// </summary>
        public Form1(World world)
        {
            if (null == world)
            {
                throw new ArgumentNullException("world");
            }
            this.world = world;
            InitializeComponent();
            this.simpleOpenGlControl1.InitializeContexts();



            world.InitGl();
            world.SetView(simpleOpenGlControl1.Height, simpleOpenGlControl1.Width);
        }

        /// <summary>
        /// Handles the resizing of the form.
        /// </summary>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            world.SetView(simpleOpenGlControl1.Height, simpleOpenGlControl1.Width);
        }

        private void simpleOpenGlControl1_Paint(object sender,
                                                PaintEventArgs e)
        {
            world.RenderScene();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            //world.UpdateLocations();
            //this.Refresh();
        }

        private void simpleOpenGlControl1_KeyDown(object sender,
            KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                world.ResetScene();
            }
        }

        private void simpleOpenGlControl1_Scroll(object sender, ScrollEventArgs e)
        {
           
        }

        private void simpleOpenGlControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                world.Zoom(0.5f, simpleOpenGlControl1.Height, simpleOpenGlControl1.Width);
            }
            else if (e.Button == MouseButtons.Right)
            {
                world.Zoom(2f, simpleOpenGlControl1.Height,simpleOpenGlControl1.Width);
            }

          
            
            Refresh();
            
        }

        private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e)
        {
          toolStripStatusLabel1.Text = e.Location.ToString()
              +Cursor.Position.ToString();
          var screenCoordinates = PointToScreen(e.Location);
            if (e.Button == MouseButtons.Left)
            {
                world.TranslateX = e.X;
                world.TranslateY = e.Y;
                world.SetView(simpleOpenGlControl1.Height, simpleOpenGlControl1.Width);
                world.UpdateCoordinate();
            }
            Refresh();
        }

        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            world.ScaleFactor = 1;
            world.SetView(simpleOpenGlControl1.Height, simpleOpenGlControl1.Width);
            Refresh();
        }
    }
}